package com.scqkxx.gamebox.activity.activities.adapter;


import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.avos.avoscloud.AVObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scqkxx.gamebox.activity.activities.utils.GlideUtils;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.utils.StringUtIls;

import java.util.List;

/**
 * sj :  2018/8/9.  17点17分
 * name: ts
 * 用途; 活动首页的适配器
 */

public class ActiveAdapter extends BaseQuickAdapter<AVObject ,BaseViewHolder> {
    private int imageWidth ;
    private int imageHegth ;

    public ActiveAdapter(int layoutResId, List<AVObject> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, AVObject item) {
        helper.addOnClickListener(R.id.tv_participate);
        final ImageView imageView = (ImageView) helper.getView(R.id.iv_active_img);
        if (imageWidth == 0 || imageHegth == 0) {
            //计算控件高度给图片设置大小
            imageView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    imageWidth = imageView.getMeasuredWidth();
                    imageHegth = imageView.getMeasuredHeight();
                    GlideUtils.loadImageSize(mContext.getApplicationContext(), StringUtIls.isEmpty(item.getString("thumbnail")), imageView, imageWidth, imageHegth);
                    imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            });
        } else {
            GlideUtils.loadImageSize(mContext.getApplicationContext(), StringUtIls.isEmpty(item.getString("thumbnail")), imageView, imageWidth, imageHegth);
        }
        helper.setText(R.id.tv_title, StringUtIls.isEmpty(item.getString("title")));
    }

}
